package com.fwtai.api;

import com.fwtai.bean.PageFormData;
import com.fwtai.datasource.DaoHandle;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统用户访问数据库
 * @作者 田应平
 * @版本 v1.0
 * @创建时间 2021年7月20日 10:42:45
 * @QQ号码 444141300
 * @Email service@yinlz.com
 * @官网 <url>http://www.fwtai.com</url>
*/
@Repository
public class ApiUserDao{

  private final DaoHandle dao;

  public ApiUserDao(final DaoHandle dao){
    this.dao = dao;
  }

  public int add(final PageFormData formData){
    return dao.execute("api_sys_user.register",formData);
  }

  @Transactional
  public int addWechat(final PageFormData formData,final long userId){
    dao.execute("api_sys_user.addUserWechat",formData);//user_info
    dao.execute("api_sys_user.addUserAccount",userId);//user_account
    return dao.execute("api_sys_user.register",formData);//sys_user
  }

  public String queryExistById(final String kid){
    return dao.queryForString("api_sys_user.queryExistById",kid);
  }

  public int edit(final PageFormData formData){
    return dao.execute("api_sys_user.edit",formData);
  }

  public int updatePhone(final PageFormData formData){
    return dao.execute("api_sys_user.updatePhone",formData);
  }

  @Transactional
  public int editVerifyPhone(final PageFormData formData){
    final int rows = dao.execute("api_sys_user.editModifyPhone",formData);//修改之前的手机号为其他
    if(rows > 0){
      final int row = dao.execute("api_sys_user.updatePhone",formData);
      return Math.max(row,0);
    }
    return rows;
  }

  @Transactional
  public Integer updateOpenid(final PageFormData formData){
    dao.execute("api_sys_user.delWechat",formData.getString("openid"));
    return dao.execute("api_sys_user.updateOpenid",formData);
  }

  public int editPortrait(final PageFormData pageFormData){
    return dao.execute("api_sys_user.editPortrait",pageFormData);
  }

  public int addPartner(final PageFormData formData){
    return dao.execute("api_sys_user.addPartner",formData);
  }

  public HashMap<String,Object> queryInfoById(final PageFormData formData){
    return dao.queryForHashMap("api_sys_user.queryInfoById",formData);
  }

  public HashMap<String,Object> queryById(final String kid){
    return dao.queryForHashMap("api_sys_user.queryById",kid);
  }

  public HashMap<String,Object> queryUserInfo(final String userId){
    return dao.queryForHashMap("api_sys_user.queryUserInfo",userId);
  }

  public List<Map<String,Object>> listData(final PageFormData formData){
    return dao.queryForListMap("api_sys_user.listData",formData);
  }

  public HashMap<String,Object> queryUser(final String username){
    return dao.queryForHashMap("api_sys_user.queryUserCode",username);
  }

  public HashMap<String,Object> queryOpenid(final String openid){
    return dao.queryForHashMap("api_sys_user.queryOpenid",openid);
  }

  public HashMap<String,Object> getUserOptions(final String loginId){
    return dao.queryForHashMap("api_sys_user.getUserOptions",loginId);
  }

  public Integer queryUserById(final String loginId){
    return dao.queryForInteger("api_sys_user.queryUserById",loginId);
  }

  public List<Map<String,Object>> getNonPayment(final String loginId){
    return dao.queryForListMap("api_sys_user.getNonPayment",loginId);
  }

  public HashMap<String, Object> getInviteInfo(final String code){
    return dao.queryForHashMap("api_sys_user.getInviteInfo",code);
  }

  public boolean getProxyType(final Long provinceId){
    return dao.queryForLong("api_sys_user.getProxyTypeProvince",provinceId) != null;
  }

  public boolean getProxyType(final Long provinceId,final Long cityId){
    final HashMap<String,Object> params = new HashMap<>(2);
    params.put("provinceId",provinceId);
    params.put("cityId",cityId);
    return dao.queryForLong("api_sys_user.getProxyTypeCity",params) != null;
  }

  public boolean getProxyType(final Long provinceId,final Long cityId,final Long countyId){
    final HashMap<String,Object> params = new HashMap<>(3);
    params.put("provinceId",provinceId);
    params.put("cityId",cityId);
    params.put("countyId",countyId);
    return dao.queryForLong("api_sys_user.getProxyTypeCounty",params) != null;
  }

  public HashMap<String, Object> getReferrer(final long loginId){
    return dao.queryForHashMap("api_sys_user.getReferrer",loginId);
  }

  public HashMap<String,Object> getUserAuthInfo(final long loginId){
    return dao.queryForHashMap("api_sys_user.getUserAuthInfo",loginId);
  }

  public Long getUserIdByCode(final String code){
    return dao.queryForLong("api_sys_user.getUserIdByCode",code);
  }

  public List<HashMap<String,Object>> getMySecurities(final PageFormData formData){
    return dao.queryForListHashMap("api_sys_user.getMySecurities",formData);
  }

  public List<HashMap<String,Object>> listReferrerUser(final PageFormData formData){
    return dao.queryForListHashMap("api_sys_user.listReferrerUser",formData);
  }

  public BigDecimal getReferrerAllIncome(final Long loginId){
    return dao.queryForBigDecimal("api_sys_user.getReferrerAllIncome",loginId);
  }

  public List<HashMap<String,Object>> getStoreSecurities(final PageFormData formData){
    return dao.queryForListHashMap("api_sys_user.getStoreSecurities",formData);
  }

  public List<HashMap<String,Object>> getApplyRecord(final PageFormData formData){
    return dao.queryForListHashMap("api_sys_user.getApplyRecord",formData);
  }

  public List<HashMap<String,Object>> listIntegral(final PageFormData formData){
    return dao.queryForListHashMap("api_sys_user.listIntegral",formData);
  }

  public HashMap<String,Object> getOrderSecurities(final long kid){
    return dao.queryForHashMap("api_sys_user.getOrderSecurities",kid);
  }

  public HashMap<String,Object> getSecurities(final long kid){
    return dao.queryForHashMap("api_sys_user.getSecurities",kid);
  }

  public int editSecurities(final HashMap<String,Object> params){
    return dao.execute("api_sys_user.editSecurities",params);
  }

  @Transactional
  public void addAccountDiscounts(final HashMap<String,Object> map,final List<HashMap<String,Object>> listDiscounts){
    dao.execute("api_buy.addStoreUserAccount",map);
    dao.execute("api_buy.add_user_account_in",listDiscounts);
  }

  public void addAccountDiscounts(final List<HashMap<String,Object>> listDiscounts){
    dao.execute("api_buy.add_user_account_in",listDiscounts);
  }

  @Transactional
  public void addAccountIn(final List<HashMap<String,Object>> listDiscounts){
    dao.execute("api_buy.updateUserAccount",listDiscounts);
    dao.execute("api_buy.add_user_account_in",listDiscounts);
  }

  @Transactional
  public void addReferrerSource(final List<HashMap<String,Object>> list){
    dao.execute("api_buy.add_user_referrer",list);
    dao.execute("api_buy.add_referrer_source",list);//核销时插入推荐人或事业部积分
  }

  public HashMap<String,Object> getAccountInfo(final long loginId){
    return dao.queryForHashMap("api_sys_user.getAccountInfo",loginId);
  }

  @Transactional
  public int addUserAccountOut(final PageFormData outRecord,final HashMap<String,Object> cash_log){
    final int rows = dao.execute("api_sys_user.addUserOutRecord",outRecord);
    if(rows > 0){
      dao.execute("api_sys_user.addUserAccountCashLog",cash_log);
    }
    return rows;
  }

  public BigDecimal getApplyRecordTotal(final long loginKey){
    return dao.queryForBigDecimal("api_sys_user.getApplyRecordTotal",loginKey);
  }

  public BigDecimal getIntegralAccount(final long loginId){
    return dao.queryForBigDecimal("api_sys_user.getIntegralAccount",loginId);
  }

  public Integer getReferrerAccount(final long loginId){
    return dao.queryForInteger("api_sys_user.getReferrerAccount",loginId);
  }

  public Integer getCategory(final long loginId){
    return dao.queryForInteger("api_sys_user.getCategory",loginId);
  }

  public HashMap<String,Object> getSecuritiesOrder(final long kid){
    return dao.queryForHashMap("api_sys_user.getSecuritiesOrder",kid);
  }

  public List<HashMap<String,Object>> getOrderDetail(final long kid){
    return dao.queryForListHashMap("api_sys_user.getOrderDetail",kid);
  }

  public List<HashMap<String,Object>> getReferrerScoreList(final PageFormData formData){
    return dao.queryForListHashMap("api_sys_user.getReferrerScoreList",formData);
  }

  public List<HashMap<String,Object>> getMyGroupIncome(final PageFormData formData){
    return dao.queryForListHashMap("api_sys_user.getMyGroupIncome",formData);
  }

  public HashMap<String,Object> getPartnerInfo(final Long userId){
    return dao.queryForHashMap("api_sys_user.getPartnerInfo",userId);
  }

  public List<HashMap<String,Object>> getMyGroupArea(final HashMap<String,Object> partnerInfo){
    return dao.queryForListHashMap("api_sys_user.getMyGroupArea",partnerInfo);
  }

  public List<HashMap<String,Object>> getAreaStoreList(final PageFormData formData){
    return dao.queryForListHashMap("api_sys_user.getAreaStoreList",formData);
  }

  public List<HashMap<String,Object>> getStoreList(final PageFormData formData){
    return dao.queryForListHashMap("api_sys_user.getStoreList",formData);
  }

  public int updateStoreStatus(final PageFormData formData){
    return dao.execute("api_sys_user.updateStoreStatus",formData);
  }

  @Transactional
  public int updateStoreCode(final PageFormData formData){
    dao.execute("api_sys_user.updateUserCode",formData);
    return dao.execute("api_sys_user.updateStoreStatus",formData);
  }

  public HashMap<String,Object> getStoreInfo(final long kid){
    return dao.queryForHashMap("api_sys_user.getStoreInfo",kid);
  }

  public boolean getProxyUser(final long userKey){
    return dao.queryForLong("api_sys_user.getProxyUser",userKey) != null;
  }

  public String getStoreCode(final long kid){
    return dao.queryForString("api_sys_user.getStoreCode",kid);
  }

  public Long getPreAudit(final long uid){
    return dao.queryForLong("api_sys_user.getPreAudit",uid);
  }

  public List<HashMap<String,Object>> getApplyAccount(final long loginId){
    return dao.queryForListHashMap("api_sys_user.getApplyAccount",loginId);
  }

  public List<HashMap<String,Object>> getAccountIn(final long loginId){
    return dao.queryForListHashMap("api_sys_user.getAccountIn",loginId);
  }

  public List<HashMap<String,Object>> getIntegralInfo(final PageFormData formData){
    return dao.queryForListHashMap("api_sys_user.getIntegralInfo",formData);
  }

  public List<HashMap<String,Object>> listIntegralInfo(final PageFormData formData){
    return dao.queryForListHashMap("api_sys_user.listIntegralInfo",formData);
  }

  public HashMap<String,Object> getOutRecord(final long kid){
    return dao.queryForHashMap("api_sys_user.getOutRecord",kid);
  }

  public List<HashMap<String,Object>> getIntegralBuy(final long out_trade_no){
    return dao.queryForListHashMap("api_sys_user.getIntegralBuy",out_trade_no);
  }

  public Integer getOrderCargo(final long kid){
    return dao.queryForInteger("api_sys_user.getOrderCargo",kid);
  }
}